package apply;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import theBeans.*;
import theDaoFactory.DaoFactory;
import theDaos.IPositionDao;

/**
 * Servlet implementation class submitServlet
 */
@WebServlet("/submitServlet")
public class submitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public submitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		int eid=Integer.parseInt(request.getParameter("eid"));
		int sid=(Integer)session.getAttribute("sid");
		
		int pid=0;//实现pid分配
		List<position> positionList=new ArrayList<position>();
		IPositionDao positionDaoInstance=DaoFactory.getPositionDaoInstance();
		positionList=positionDaoInstance.findByIfEmpty();
		if(positionList!=null)
		{
			pid=(positionList.get(0)).getPid();
		
			//将eid,pid封装成exam对象发送进行二次审查
			exam newExam=new exam();
			newExam.setEid(eid);
			newExam.setPid(pid);
			newExam.setSid(sid);
			session.setAttribute("newExam", newExam);
			response.sendRedirect("checkServlet");
		}
		else
		{
			request.setAttribute("errorInfo", "");
			request.setAttribute("toURL", "");
			request.getRequestDispatcher("errorPage.jsp").forward(request, response);
		}
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
